package com.wyl.template.controller;

import com.wyl.template.aop.annotation.SysLogger;
import com.wyl.template.domain.common.ResponseResult;
import com.wyl.template.domain.model.SysPermissionDO;
import com.wyl.template.domain.model.SysUserDO;
import com.wyl.template.domain.vo.PermissionVO;
import com.wyl.template.enums.LoggerType;
import com.wyl.template.exception.ErrorCodeEnum;
import com.wyl.template.service.SysPermissionService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Auther: wyl
 * @Date: 2021/6/30
 * @Description:
 */
@SysLogger(group = "权限")
@RestController
@RequestMapping("/permission")
@Slf4j
public class PermissionController {

    @Autowired
    private SysPermissionService permissionService;


    /**
     * 获得所有权限列表
     * 获得权限内的，防止越权
     *
     * @return
     */
    @SysLogger(name = "查询菜单列表", type = LoggerType.SELECT)
    @GetMapping("getMenus")
    public ResponseResult getMenus() {
        Subject subject = SecurityUtils.getSubject();
        SysUserDO user = (SysUserDO) subject.getPrincipal();
        return ResponseResult.success(permissionService.listUserPermissions(user.getId()));
    }

    /**
     * 获得权限内的权限列表
     * 防止越权
     *
     * @return
     */
    @SysLogger(name = "查询权限列表", type = LoggerType.SELECT)
    @GetMapping("permission")
    public ResponseResult getPermission() {
        Subject subject = SecurityUtils.getSubject();
        SysUserDO user = (SysUserDO) subject.getPrincipal();
        return ResponseResult.success(permissionService.listUserPermissions(user.getId()));
    }

    /**
     * 新增目录
     *
     * @param permissionDO
     * @return
     */
    @SysLogger(name = "新增", type = LoggerType.ADD)
    @PostMapping("permission")
    public ResponseResult addPermission(@RequestBody SysPermissionDO permissionDO) {

        return saveOrUpdate(permissionDO);
    }

    /**
     * 更新目录
     *
     * @param permissionDO
     * @return
     */
    @SysLogger(name = "修改", type = LoggerType.UPDATE)
    @PutMapping("permission")
    public ResponseResult updatePermission(@RequestBody SysPermissionDO permissionDO) {

        return saveOrUpdate(permissionDO);
    }

    /**
     * 删除目录
     * @param permissionDO
     * @return
     */
    @SysLogger(name = "删除", type = LoggerType.DELETE)
    @DeleteMapping("permission")
    public ResponseResult deletePermission(@RequestBody SysPermissionDO permissionDO) {
        if (permissionDO == null) {
            return ResponseResult.error(ErrorCodeEnum.DELETE_FAILURE);
        }
        return permissionService.deletePermisson(permissionDO);
    }

    /**
     * 目录排序
     *
     * @param permissionVO
     * @return
     */
    @SysLogger(name = "菜单排序", type = LoggerType.UPDATE)
    @PostMapping("sortPermission")
    public ResponseResult sortPermission(@RequestBody PermissionVO permissionVO) {
        return permissionService.sortPermission(permissionVO.getDropId(), permissionVO.getDragId());
    }

    /**
     * 保存 或者 更新权限
     *
     * @param permissionDO
     * @return
     */
    private ResponseResult saveOrUpdate(SysPermissionDO permissionDO) {
        if (permissionDO == null) {
            return ResponseResult.error(ErrorCodeEnum.SAVE_ERROR);
        }
        return permissionService.savePermission(permissionDO);
    }
}
